<?php 
class Controller_Payment_WeiXin extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = 'smarty:payment/blank';
        parent::__construct($req, $res);
    }

    public function action_order ()
    {
        $orderNo = Arr::get($this->_query, 'orderNo', '');
        if (empty($orderNo))
            exit(0);

        $orderDetail = App_Order::detail ($orderNo);
        $orderTitle  = Arr::get($orderDetail, 'title');
        $orderMoney  = Arr::get($orderDetail, 'moneyTotal', 0);

		ini_set('date.timezone','Asia/Shanghai');
		require_once DOCROOT."/resource/wxpay/lib/WxPay.Api.php";
		require_once DOCROOT."/resource/wxpay/example/WxPay.JsApiPay.php";

		
		//①、获取用户openid
		$tools = new JsApiPay();
		$openId = $tools->GetOpenid();
		
		//②、统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody(utf8::substr($orderTitle, 0, 30));
		$input->SetAttach("test");
		$input->SetOut_trade_no($orderNo);
		$input->SetTotal_fee($orderMoney);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("test");
		$input->SetNotify_url(url::site("/payment/weixin/notify", 'http', FALSE));
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
		$order = WxPayApi::unifiedOrder($input);
		$jsApiParameters = $tools->GetJsApiParameters($order);
		
		//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
		/**
		 * 注意：
		 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
		 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
		 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
		 */
		
        $this->template->set_filename('smarty:payment/wxpay');
        Kohana::$log->add(Log::DEBUG, 'paymentParam: ' . $jsApiParameters);
        $this->template->set('paymentParam', $jsApiParameters);
        $this->template->set('orderMoney', number_format($orderMoney/100, 2));
        $this->template->set('orderNo', $orderNo);
    }


    public function action_test ()
    {
        //$orderNo = '2015062010258001';

        //$pay = Payment::factory('weixin');
        //$paymentJsApiParams = $pay->getParametersByOrder($orderNo);

		ini_set('date.timezone','Asia/Shanghai');
		//error_reporting(E_ERROR);
		require_once DOCROOT."/resource/wxpay/lib/WxPay.Api.php";
		require_once DOCROOT."/resource/wxpay/example/WxPay.JsApiPay.php";
		//require_once DOCROOT."/resource/wxpay/example/log.php";
		
		//初始化日志
		//$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
		//$log = Log::Init($logHandler, 15);
		
		
		//①、获取用户openid
		$tools = new JsApiPay();
		$openId = $tools->GetOpenid();
		
		//②、统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody("test");
		$input->SetAttach("test");
		$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
		$input->SetTotal_fee("1");
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("test");
		$input->SetNotify_url(url::site("/payment/weixin/notify", 'http', FALSE));
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
		$order = WxPayApi::unifiedOrder($input);
		//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
		//printf_info($order);
		$jsApiParameters = $tools->GetJsApiParameters($order);
		
		//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
		/**
		 * 注意：
		 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
		 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
		 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
		 */
		
        $this->template->set_filename('smarty:payment/wxpay');
        $this->template->set('paymentParam', $jsApiParameters);
        $this->template->set('orderMoney', '0.01');

    }

    public function action_notify ()
    {
        $notify = new Payment_Weixin_Notify();
        $notify->Handle(false);
    }

	//打印输出数组信息
	private function printf_info($data)
	{
	    foreach($data as $key=>$value){
	        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
	    }
	}

}
